//
//  CommentCellViewModel.m
//  CommentPage
//
//  Created by 欧杨 on 2020/11/28.
//

#import "CommentCellViewModel.h"
#import <AFHTTPSessionManager.h>
#import "AppDelegate.h"

@interface CommentCellViewModel()

@property(strong, nonatomic) Comment *comment;

@end


@implementation CommentCellViewModel

- (instancetype)initWithComment:(Comment *)comment
{
    if (self = [super init]) {
        AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
        self.comment = comment;
        //self.avater;
        self.content = _comment.comment.Content;
        //self.isSelf = (_comment.comment.UserID == myDelegate.userInfo.ID);
        self.likeNum = _comment.comment.LikeNum;
        self.replyNum = _comment.replies.count;
        self.userName = _comment.user.Name;
        self.date = [NSDate dateWithTimeIntervalSince1970:_comment.comment.Date/1000];
    }
    return self;
}

- (void)likeClicked
{
    NSDictionary *postParam = @{@"isContent":@NO, @"isComment":@YES, @"isReply":@NO};
    NSString *commentID = self.comment.comment.ID;
    NSString *URLString = [NSString stringWithFormat:@"%@%@", @"http://172.18.178.56/api/like/", commentID];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    if (!self.isLike){
        [manager POST:URLString parameters:postParam progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"点赞 %@",responseObject);
            if ([responseObject[@"State"]  isEqual: @"success"]) {
                self.comment.comment.LikeNum++;
                self.likeNum = self.comment.comment.LikeNum;
                self.isLike = !self.isLike;
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"failed");
        }];
    } else {
        [manager PATCH:URLString parameters:postParam success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"取消点赞%@",responseObject);
            if ([responseObject[@"State"]  isEqual: @"success"]) {
                self.comment.comment.LikeNum--;
                self.likeNum = self.comment.comment.LikeNum;
                self.isLike = !self.isLike;
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"failed");
        }];
    }
}

-(void)addNewReply:(NSString *)reply
{
    NSString *URLString = [NSString stringWithFormat:@"%@", @"http://172.18.178.56/api/comment"];
    NSDictionary *postParam = @{@"contentId":self.comment.comment.ID,
                                @"fatherId":self.comment.comment.UserID,
                                @"content":reply,
                                @"isReply":@YES};
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager POST:URLString parameters:postParam progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"success");
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];
}

@end
